new Vue({
    el: '#app',
    data() {
        return {
            Purchase_Order_SerialNumber: "",
            Wares: [],
            formAcceptance: {
                Children:{
                    purchase_order_detail:[]
                }
            },
            formware: {
                Purchase_Receipt_SerialNumber: "",
                Receipt_Date: "",
                Receipt_Type: "",
                Store_SerialNumber: "",
                Children: {
                    purchase_receipt_detail: [{
                        Purchase_Receipt_Detail_SerialNumber: "",
                        Purchase_Receipt_SerialNumber: "",
                        Purchase_Order_SerialNumber: "",
                        Article_SerialNumber: "",
                        Receipt_Quantity: "",
                        Amount: ""
                    }]
                }
            },
            ArticleAndBrand:[],
            rules: {
                Store_SerialNumber: [{ required: true, message: '请选择仓库', trigger: 'change' }],
                Receipt_Type: [{ required: true, message: '请选择存货地点', trigger: 'change' }],
            },
            loading: false,
        }
    },
    mounted() {
        this.Purchase_Order_SerialNumber = sessionStorage.getItem('Purchase_Order_SerialNumber')
        this.getGoods(this.Purchase_Order_SerialNumber)
        this.GetArticleAndBrand(this.Purchase_Order_SerialNumber)
    },
    computed:{
        ArticleName(){
            return function (id){
                let str = ''
                if (id) {
                    if (this.ArticleAndBrand.some(item=>item.Purchase_Order_Detail_SerialNumber==id)) {
                        let obj = this.ArticleAndBrand.filter(item=>item.Purchase_Order_Detail_SerialNumber==id)[0]
                        return obj.Article_Name
                    }
                    return str
                }else{
                    return str
                }
            }
        },
        Brand_Name(){
            return function (id){
                if (id) {
                    let obj = this.ArticleAndBrand.filter(item=>item.Purchase_Order_Detail_SerialNumber==id)[0]
                    return obj.Brand_Name
                }else{
                    return ''
                }
            }
        },
        Warehouse_Name(){
            return function (id){
                if (id) {
                    let obj = this.ArticleAndBrand.filter(item=>item.Purchase_Order_Detail_SerialNumber==id)[0]
                    return obj.Warehouse_Name
                }else{
                    return ''
                }
            }
        }
    },
    methods: {
        getGoods(Purchase_Order_SerialNumber) {//获取所选商品
            let this_ = this
            $.ajax({
                url: DomainName + '/getSubtableInquiryOriginal?token=' + $.cookie("token"),
                type: 'POST',
                data: {
                    Primary_Key: Purchase_Order_SerialNumber,
                    JSONObject: JSON.stringify({ purchase_order: { purchase_order_detail: {} } }),
                },
                success: function (res) {
                    console.log(res)
                    if (res.purchase_order.length == 1) {
                        this_.formAcceptance = res.purchase_order[0]
                        this_.formware.Children.purchase_receipt_detail = []
                        this_.formAcceptance.Children.purchase_order_detail.forEach(item => {
                            item.Receipt_Quantity = item.Quantity
                            this_.formware.Children.purchase_receipt_detail.push({
                                Purchase_Receipt_Detail_SerialNumber: "",
                                Purchase_Receipt_SerialNumber: "",
                                Purchase_Order_SerialNumber: item.Purchase_Order_SerialNumber,
                                Article_SerialNumber: item.Article_SerialNumber,
                                Receipt_Quantity: item.Receipt_Quantity,
                                Amount: item.Article_UnitPrice,
                            })
                        })
                    }
                    console.log(this_.formAcceptance)
                },
                dataType: "json"
            });
        },
        ModifyGoods() {//修改商品
            let this_ = this
            this.loading = true
            this.formAcceptance.Row_Status = 6
            this.formAcceptance.Children.purchase_order_detail.forEach(item => {
                item.Not_Receipt_Quantity = item.Quantity - item.Receipt_Quantity
            })
            let data = {
                Data: JSON.stringify(
                    {
                        Modify: { 'purchase_order': [this.formAcceptance] },
                    }
                )
            }
            $.ajax({
                url: DomainName + '/getEditAction?token=' + $.cookie("token"),
                type: 'POST',
                data,
                success: function (res) {
                    console.log(res)
                    if (res.Status == 'success') {
                        this_.$message({
                            message: '验收成功',
                            type: 'success'
                        })
                        this_.back()
                    }
                },
                complete: function (XMLHttpRequest) {
                    this_.loading = false
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                },
                dataType: "json"
            });
        },
        back() {
            window.history.go(-1)
        },
        GetArticleAndBrand(Purchase_Order_SerialNumber){
            const this_ = this
            $.ajax({
                url: DomainName + '/getPurchaseOrderDetailIDAction?token=' + $.cookie("token"),
                type: 'POST',
                data:{
                    Purchase_Order_SerialNumber,
                },
                success: function (res) {
                    console.log(res)
                    this_.ArticleAndBrand = res.Data
                },
                complete: function (XMLHttpRequest) {
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                },
                dataType: "json"
            });
        },
        Pricethousands(num){//转千分位
            return thousands(num)
        },
    },
})